package com.cookor.poi.modules.phy.mobile;

import com.cookor.poi.common.beanvalidator.BeanValidators;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.entity.TPatient;
import com.cookor.poi.modules.phy.service.TPatientService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/phy/patient"})
@ResponseBody
public class ApiPatientController extends ApiBaseController {
  @Autowired
  private TPatientService tPatientService;
  
  @RequestMapping({"/save"})
  public ResultUtil savePatient(TPatient tPatient) {
    ResultUtil result = getResult();
    try {
      beanValidator(tPatient, new Class[0]);
      TPatient query = new TPatient();
      query.setTenantId(tPatient.getTenantId());
      query.setOpenId(tPatient.getOpenId());
      query.setCard(tPatient.getCard());
      List<TPatient> list = this.tPatientService.findList(query);
      if (list.size() > 0 && (tPatient.getId() == null || !tPatient.getId().equals(((TPatient)list.get(0)).getId())))
        throw new Exception("已存在身份证号重复的就诊人！"); 
      this.tPatientService.save(tPatient);
    } catch (ConstraintViolationException ex) {
      this.logger.error("ApiPatientController addPatient ConstraintViolationException!", (Throwable)ex);
      List<String> list = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
      list.add(0, "数据验证失败：");
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(list.toString());
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiPatientController savePatient error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/list"})
  public ResultUtil patientList(HttpServletRequest request, String openId) {
    ResultUtil result = getResult();
    try {
      if (StringUtils.isBlank(openId))
        throw new Exception("参数有误，请退出公众号后重试！"); 
      Integer tenantId = getTenantId(request);
      TPatient query = new TPatient();
      query.setTenantId(tenantId);
      query.setOpenId(openId);
      query.setDelFlag("0");
      List<TPatient> list = this.tPatientService.findList(query);
      result.putParams("list", list);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiPatientController patientList error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/dele"})
  public ResultUtil patientList(HttpServletRequest request, TPatient tPatient) {
    ResultUtil result = getResult();
    try {
      getTenantId(request);
      this.tPatientService.delete(tPatient);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiPatientController dele error!", e);
    } 
    return result;
  }
}
